package com.bizfx.component.fields
{
	import com.bizfx.component.views.IDetailView;
	
	import mx.controls.Image;
	import mx.events.FlexEvent;

	public class ImageField extends Image implements IField
	{
		private var dataDirty:Boolean = false;
		
		public function ImageField()
		{
		}
		
		[Bindable]
		public function get fieldKey():String
		{
			return this.id;
		}
		
		public function set fieldKey(value:String):void
		{
			if (this.id != value) {
				this.id = value;
			}
		}
		
		private var _bindProperty:String;
		[Bindable("bindPropertyChange")]
		public function get bindProperty():String
		{
			return _bindProperty;
		}
		
		public function set bindProperty(value:String):void {
			if (this._bindProperty != value) {
				_bindProperty = value;
				this.dispatchEvent(new Event("bindPropertyChange"));
			}
		}
		
		private var _data:Object;
		[Bindable("dataChange")]
		[Inspectable(category="General")]
		override public function get data():Object {
			return this._data;
		}
		
		override public function set data(value:Object):void {
			if (this._data != value) {
				this.dataDirty = true;
				this._data = value;
				this.source = value;
				this.invalidateDisplayList();
				dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
			}
		}
		
		[Inspectable(category="General", enumeration="true,false", defaultValue="false")]
		[Bindable("readOnlyChanged")]
		public function get readOnly():Boolean {
			return this.enabled;
		}
		
		public function set readOnly(value:Boolean):void {
			if (this.enabled == value) {
				this.enabled = !value;
				this.dispatchEvent(new Event("readOnlyChanged"));
			}
		}
		
//		private var _view:IDetailView;
//		
//		public function get view():IDetailView
//		{
//			return this._view;
//		}
//		
//		public function set view(value:IDetailView):void {
//			if (this._view != value) {
//				if (_view) {
//					_view.removeFieldByKey(this.fieldKey);
//				}
//				this._view = value;
//				if (_view) {
//					_view.addField(this);
//				}
//			}
//		}
		
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			if (this.dataDirty) {
				this.dataDirty = false;
				this.source = this._data;
			}
		}
	}
}